package com.example.scaloovka;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends ActionBarActivity {

	protected static final String TAG = "ScaloovkaMain";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_edit) {
			Log.d(TAG, "Action edit chosen");
			Intent intent = new Intent(this, EditActivity.class);
			startActivity(intent);
			return true;
		}
		if (id == R.id.action_info) {
			Log.d(TAG, "Action info chosen");
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}
		
		private String currentUnits;
		private EditText valueInput;
		private EditText scaleInput;
		private EditText[] scales;
		private EditText[] results;
		private Spinner unitsSpinner;

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
			this.unitsSpinner = (Spinner) rootView
					.findViewById(R.id.unitSpinner);
			currentUnits = (String) unitsSpinner.getSelectedItem();

			this.unitsSpinner
					.setOnItemSelectedListener(new UnitChoiceListener());

			this.scaleInput = (EditText) rootView.findViewById(R.id.scaleInput);
			this.scaleInput
					.setOnFocusChangeListener(new InputValidationListener());

			this.valueInput = (EditText) rootView.findViewById(R.id.valueInput);
			this.valueInput
					.setOnFocusChangeListener(new InputValidationListener());
			
			EditText scale1 = (EditText) rootView.findViewById(R.id.scale1);
			EditText scale2 = (EditText) rootView.findViewById(R.id.scale2);
			EditText scale3 = (EditText) rootView.findViewById(R.id.scale3);
			EditText scale4 = (EditText) rootView.findViewById(R.id.scale4);
			this.scales = new EditText[] { scale1, scale2, scale3, scale4 };
			EditText result1 = (EditText) rootView.findViewById(R.id.result1);
			EditText result2 = (EditText) rootView.findViewById(R.id.result2);
			EditText result3 = (EditText) rootView.findViewById(R.id.result3);
			EditText result4 = (EditText) rootView.findViewById(R.id.result4);
			this.results = new EditText[] { result1, result2, result3, result4 };

			updateScalesViews();

			return rootView;
		}

		@Override
		public void onResume() {
			super.onResume();
			updateScalesViews();
			Double scale = ScalesMap.getInstance().getScale();
			Double value = ScalesMap.getInstance().getValue();
			unitsSpinner.setSelection(ScalesMap.getInstance().getCurrentUnitsPostion());
			currentUnits = (String)unitsSpinner.getItemAtPosition(ScalesMap.getInstance().getCurrentUnitsPostion());
			scaleInput.setText(scale.toString());
			valueInput.setText(value.toString());
			calculate(scale, value, currentUnits);
		}

		private void updateScalesViews() {
			double[] scaleVals = ScalesMap.getInstance().getScales();
			for (int i = 0; i < this.scales.length; i++) {
				this.scales[i].setText(String.valueOf(scaleVals[i]));
			}
		}

		private void calculate(double scale, double value, String unit) {
			String[] resultVals;
			resultVals = Calculator.calculate(scale, value, unit);
			for (int i = 0; i < resultVals.length; i++) {
				this.results[i].setText(resultVals[i]);
			}
		}

		private class UnitChoiceListener implements OnItemSelectedListener {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				currentUnits = (String) parent.getItemAtPosition(position);
				ScalesMap.getInstance().setCurrentUnitsPostion(parent.getSelectedItemPosition());
				Double scale = Double.valueOf(scaleInput.getText().toString());
				Double value = Double.valueOf(valueInput.getText().toString());
				calculate(scale, value, currentUnits);
				ScalesMap.getInstance().setScale(scale);
				ScalesMap.getInstance().setValue(value);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				parent.setSelection(parent.getSelectedItemPosition());
			}
		}

		private class InputValidationListener implements OnFocusChangeListener {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					Double scale = Double.valueOf(scaleInput.getText()
							.toString());
					Double value = Double.valueOf(valueInput.getText()
							.toString());
					Log.d(TAG, "Currents units: " + currentUnits);
					Log.d(TAG, "ScaleInput: " + scale);
					Log.d(TAG, "ValueInput: " + value);
					calculate(scale, value, currentUnits);
					ScalesMap.getInstance().setScale(scale);
					ScalesMap.getInstance().setValue(value);
				}
			}
		}

	}

}
